<?php

namespace App\Admin\Controllers;

use App\Models\ContentCategory;
use App\Models\Content;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ContentCategoryController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文档分类';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ContentCategory(), function (Grid $grid) {
            $grid->model()->whereNull('deleted_at');
            //$grid->column('id', __('ID'));
            $grid->column('sn');
            $grid->column('primary_id')->display(function ($primary_id) {
                return ContentCategory::getfield(['sn' => $primary_id]);
            });
            $grid->column('name');
            $grid->column('lang')->display(function ($lang) {
                return Content::$lib_lang[$lang];
            });
            $grid->column('weight');
            $server = '';
//            $grid->column('id', __('轮播'))->display(function () use ($server) {
//                $msg = "<a href='" . $server . "/admin/contents?cat_id=$this->sn'>轮播</a>";
//                return $msg;
//            });
            $grid->column('created_at')->display(function ($created_at) {
                return $created_at;
            });
            $grid->actions(function ($actions) {
//            $actions->disableDelete();
                $actions->disableView();
//            $actions->add(new Delete());
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('sn')->width(3);
                $filter->equal('name')->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new  ContentCategory(), function (Show $show) {
            $show->field('id');
            $show->field('sn');
            $show->field('primary_id');
            $show->field('name');
            $show->field('lang');
            $show->field('weight');
            $show->field('created_at');
            $show->field('updated_at');
        });

    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ContentCategory(), function (Form $form) {
            $arr = request()->route()->parameters();
            if (isset($arr["content_category"])) {
                $id = $arr["content_category"];
            } else {
                $id = 0;
            }
            $form->text('sn')->required();
            $form->select('primary_id')->options('/content-categories/category-list?typeid=' . $id);
            $form->text('name')->required();
            $form->select('lang')->options(Content::$lib_lang)->required();
            $form->number('weight')->default(1);
            $form->radio('show', '首页显示')->options(tt_status('', true))->default('1');
        });
    }

    public function categoryList(ContentCategory $contentCategory, Request $request)
    {
        $id = $request->input('typeid');
        $where = [];
        if ($id && $id != 0) {
            $where = [$id];
        }
        $result = $contentCategory->getlist($where);
        if ($result) {
            foreach ($result as $k => &$val) {
                $val['id'] = $val['ids'];
            }
        }
        return response()->json($result);
    }
}
